import { obj_str, uPlc } from 'sui';
import { g_line, g_wsn } from 'db/state';

const get_step_str = (data, k) => obj_str(data, k);

const send_step = (fdone, st, okfun, errfun) => {
    const cmd = {
        line: g_line(),
        sn: g_wsn(),
        v: st
    };

    uPlc(null, "put", cmd, r => {
        if (okfun) {
            okfun();
        }

        if (fdone) {
            fdone();
        }
    }, e => {
        if (errfun) {
            errfun();
        }

        if (fdone) {
            fdone();
        }
    });
};

const clear_gun = (watch) => {
    if (watch) {
        send_step(watch.startWatch, {gun: ""});
    }
    else {
        send_step(null, {gun: ""});
    }
};

const send_gun = (v) => {
    send_step(null, {gun: v});
};

const get_gun = (step) => get_step_str(step, "gun");

const get_mod = (mod, ws="") => {
    let wsn = g_wsn();
    if (ws) {
        wsn = ws;
    }

    return `${mod}_${wsn}`;
};

export {send_gun, get_mod, get_step_str, get_gun, clear_gun};
